using UnityEngine;
using System.Collections;

/// \class  gkSpawnTrigger
/// \brief  Base class for spawn trigger
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawnTrigger")]
public class gkSpawnTrigger : MonoBehaviour
{
    /// The minimum number of spawn
    public int NbSpawnMin = 1;

    /// The maximum number of spawn
    public int NbSpawnMax = 1;

    /// The spawner
    private gkSpawner m_rSpawner;

    /// \brief  Called just after the game begin to play
    protected virtual void Start()
    {
	    // Get the owner
	    m_rSpawner = GetComponent<gkSpawner>();
    }

    /// \brief  Called each frame
    void Update()
    {
	    UpdateSpawn();
    }

    /// \brief  Update the spawning process
    protected virtual void UpdateSpawn()
    {
	    if(CanSpawn())
	    {
		    // Spawn the actors
		    SpawnActors();
	    }
    }

    /// \brief  Spawn randomly actors
    protected virtual void SpawnActors()
    {
	    int iNbActorToSpawned;
	    int i; 
	    // Select the number of actor to spawn
        iNbActorToSpawned = Random.Range(NbSpawnMin, NbSpawnMax);

	    // Spawn the selected number of actor
	    for(i = 0; i<iNbActorToSpawned; i++)
	    {
		    OnSpawnActor(m_rSpawner.SpawnActor(i, iNbActorToSpawned));
	    }
    }

    /// \brief  Can we spawn again
    protected virtual bool CanSpawn()
    {
	    return true;
    }

    /// \brief  Called at each actor spawn
    protected virtual void OnSpawnActor(GameObject a_rSpawnedActor)
    {}
}